﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SStateMachine
{
    public partial class StateMachine<TState, TTrigger>
    {
        public class StateConfigure
        {
            private readonly TState state;
            private readonly StateMachine<TState, TTrigger> stateMachine;
            private readonly StateEvents stateEvents;

            public StateConfigure(TState state, StateMachine<TState, TTrigger> stateMachine, StateEvents stateEvents)
            {
                this.state = state;
                this.stateMachine = stateMachine;
                this.stateEvents = stateEvents;
            }

            public StateConfigure AddTrigger(TTrigger trigger, TState destination)
            {
                stateEvents.AddTrigger(trigger, destination);
                stateMachine.AddTrigger(state, trigger, stateEvents.GetTransition(trigger));

                if (!stateMachine.states.ContainsKey(destination))
                {
                    stateMachine.states.Add(destination, null);
                }

                return this;
            }

            public StateConfigure OnEnter(Action EnterAction)
            {
                if (EnterAction == null)
                    throw new ArgumentNullException();

                stateEvents.AddEnterEvent(EnterAction);
                return this;
            }

            public StateConfigure OnExit(Action ExitAction)
            {
                if (ExitAction == null)
                    throw new ArgumentNullException();

                stateEvents.AddExitEvent(ExitAction);
                return this;
            }
        }
    }
}
